import QtQuick
import QtQuick.Controls
import Qt.labs.platform 1.1
import FluentUI 1.0
import "../views"
import QtCore

ApplicationWindow {
    visible: true
    width: Constants.windowWidth
    height: Constants.windowHeight
    title: "App Demo"
    
    Text {
        text: qsTr("Hello UntitledProject")
        anchors.verticalCenterOffset: -136
        anchors.horizontalCenterOffset: -75
        anchors.centerIn: parent
    }
    
    GridView {
        id: gridView
        x: 50
        y: 50
        width: 146
        height: 137
        model: ListModel {
            ListElement {
                name: "Grey"
                colorCode: "grey"
            }

            ListElement {
                name: "Red"
                colorCode: "red"
            }

            ListElement {
                name: "Blue"
                colorCode: "blue"
            }

            ListElement {
                name: "Green"
                colorCode: "green"
            }
        }
        delegate: Item {
            x: 5
            height: 50
            Column {
                spacing: 5
                Rectangle {
                    width: 40
                    height: 40
                    color: colorCode
                    anchors.horizontalCenter: parent.horizontalCenter
                }

                Text {
                    x: 5
                    text: name
                    font.bold: true
                    anchors.horizontalCenter: parent.horizontalCenter
                }
            }
        }
        cellWidth: 70
        cellHeight: 70
    }
    
    Button {
        id: button
        x: 50
        y: 200
        text: qsTr("边缘检测")
        onClicked: {
            fileDialog.open()
        }
    }

      FileDialog {
        id: fileDialog
        title: "选择要处理的图片"
        folder: StandardPaths.writableLocation(StandardPaths.PicturesLocation)
        nameFilters: ["Image files (*.jpg *.png *.bmp *.jpeg)"]
        
        onAccepted: {
            // 使用fileUrl而不是fileDialog.fileUrl
            var selectedFileUrl = fileUrl
            console.log("Selected file URL:", selectedFileUrl)
            
            if (!selectedFileUrl || selectedFileUrl.toString() === "") {
                statusText.text = "未选择有效的图片文件"
                return
            }
            
            // 直接使用文件URL
            edgeImage.source = selectedFileUrl
            statusText.text = "正在处理图片..."
        }
        
        onRejected: {
            statusText.text = "取消选择图片"
        }
    }

    Text {
        id: statusText
        anchors.bottom: parent.bottom
        anchors.horizontalCenter: parent.horizontalCenter
        color: "red"
    }

    Image {
        id: edgeImage
        x: 250
        y: 200
        width: 200
        height: 200
        fillMode: Image.PreserveAspectFit
    }

    // 文件辅助组件
    Component {
        id: fileHelperComponent
        QtObject {
            property string filePath: ""
            property bool exists: false
            
            Component.onCompleted: {
                var file = Qt.createQmlObject(
                    'import QtQuick 2.0; import QtCore 2.0; ' +
                    'File { source: "' + filePath + '" }',
                    this
                )
                exists = file.exists
                file.destroy()
            }
        }
    }
}